package com.orange.quickback.system.service.user.manage.impl;

import com.orange.quickback.system.api.common.result.QBootResult;
import com.orange.quickback.system.api.user.manage.result.QBootRegisterResult;
import com.orange.quickback.system.dao.user.manage.QBootRoleRepository;
import com.orange.quickback.system.dao.user.manage.QBootUserRepository;
import com.orange.quickback.system.entities.user.manage.QBootLoginInfo;
import com.orange.quickback.system.entities.user.manage.QBootRole;
import com.orange.quickback.system.entities.user.manage.QBootUser;
import com.orange.quickback.system.entities.user.manage.QBootUserInfo;
import com.orange.quickback.system.service.user.manage.QBootUserService;
import com.orange.quickback.system.service.user.manage.views.UserInfoViews;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QBootUserServiceImpl implements QBootUserService {
    @Resource
    QBootUserRepository qBootUserRepository;
    @Resource
    QBootRoleRepository qBootRoleRepository;

    @Override
    public QBootResult registerQBootUser(String email,String userName,String nickName,String password,String phoneNumber,String avatar,String introduction) {
        try {
            QBootUser qBootUser = new QBootUser();
            qBootUser.setEMail(email);
            qBootUser.setNickName(nickName);
            qBootUser.setLocked(false);
            qBootUser.setEnabled(true);
            qBootUser.setUserName(userName);
            qBootUser.setPassword(new BCryptPasswordEncoder().encode(password));
            qBootUser.setPhoneNumber(phoneNumber);
            List<QBootRole> roles = new ArrayList<QBootRole>();
            QBootRole qBootUserRole = qBootRoleRepository.findByRoleName("ROLE_user");
            qBootUser.setQBootUserInfo(new QBootUserInfo(avatar, introduction));
            roles.add(qBootUserRole);
            qBootUser.setRole_users(roles);
            qBootUserRepository.save(qBootUser);
            return QBootResult.build("注册成功！");
        }catch (Exception e){
            return QBootResult.buildFail("注册失败：" + e.getMessage());
        }
    }

    @Override
    public QBootResult getAllUsersByPage(int page,int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        Page<QBootUser> qBootUsers = qBootUserRepository.findAll(pageRequest);
        qBootUsers.getContent().forEach(it->it.setPassword(null));
        return QBootResult.build(qBootUserRepository.findAll(pageRequest));
    }

    @Override
    public QBootLoginInfo login(String eMail, String passWord){
        QBootLoginInfo qBootLoginInfo = new QBootLoginInfo();
        return qBootLoginInfo;
    }
    @Override
    public QBootResult getUserInfo(String username){
        QBootUser qBootUser = qBootUserRepository.findByUserName(username);
        List<String> roleList = new ArrayList<>();
        for(QBootRole role : qBootUser.getRole_users()){
            roleList.add(role.getRoleName());
        }
        QBootUserInfo qBootUserInfo = qBootUser.getQBootUserInfo();
        UserInfoViews userInfoViews = new UserInfoViews(qBootUser.getNickName(),roleList,qBootUserInfo.getAvatar(),qBootUserInfo.getIntroduction());
        return QBootResult.build(userInfoViews);
    }
    public QBootResult initRegister(){
        try {
            QBootUser qBootUser = new QBootUser();
            qBootUser.setEMail("1243137612@qq.com");
            qBootUser.setNickName("一只没梦想的桔子");
            qBootUser.setLocked(false);
            qBootUser.setEnabled(true);
            qBootUser.setUserName("qboot");
            qBootUser.setPassword(new BCryptPasswordEncoder().encode("123456"));
            qBootUser.setPhoneNumber("13888888888");
            List<QBootRole> roles = new ArrayList<QBootRole>();
            QBootRole qBootRole = new QBootRole(2L,"ROLE_admin", "系统管理员");
            qBootUser.setQBootUserInfo(new QBootUserInfo("","这个人很神秘，啥都没写"));
            roles.add(qBootRole);
            qBootUser.setRole_users(roles);
            qBootRoleRepository.save(new QBootRole(1L,"ROLE_dba", "数据库管理员"));
            qBootRoleRepository.save(new QBootRole(2L,"ROLE_admin", "系统管理员"));
            qBootRoleRepository.save(new QBootRole(3L,"ROLE_user", "用户"));
            qBootRoleRepository.save(new QBootRole(4L,"ROLE_ANONYMOUS", "游客"));
            qBootUserRepository.save(qBootUser);
        }catch (Exception e){
            e.printStackTrace();
            return QBootResult.build(new QBootRegisterResult("创建失败","500","ROLE_dba"));
        }
        return QBootResult.build(new QBootRegisterResult("创建成功","200","ROLE_dba"));
    }
}
